package com.yungu.swift.socket.communication.server.handler.strategy;

import com.google.gson.Gson;
import com.yungu.swift.socket.communication.message.HeartBeatMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

/**
 * 客户端心跳响应
 *
 * @author : cuixiuyin
 * @date : 2019/8/30
 */
public class HeartBeatHandler implements MessageHandler {

    @Override
    public void handler(ChannelHandlerContext channelHandlerContext, String text) {
        Gson gson = new Gson();
        //收到心跳，空闲计数清零
        HeartBeatMessage heartBeatMessage = gson.fromJson(text, HeartBeatMessage.class);
        //服务端响应心跳
        HeartBeatMessage heartBeatResponse = new HeartBeatMessage(heartBeatMessage.getClientUuid());
        channelHandlerContext.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(heartBeatResponse)));
    }
}
